package com.see.viewer.application;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.see.domain.dao.DataNucleusUtil;
import com.see.domain.model.StaticData;
import com.see.viewer.data.StaticDataAdaptor;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.event.Action;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.Table.CellStyleGenerator;
import com.vaadin.ui.VerticalLayout;

@SuppressWarnings("serial")
public class TableLayout extends VerticalLayout {

	static Table table = new Table("");

	HashSet<Object> markedRows = new HashSet<Object>();

	static final Action ACTION_MARK = new Action("Mark");
	static final Action ACTION_UNMARK = new Action("Unmark");
	static final Action ACTION_LOG = new Action("Save");

	static final Action[] ACTIONS_UNMARKED = new Action[] { ACTION_MARK,
			ACTION_LOG };
	static final Action[] ACTIONS_MARKED = new Action[] { ACTION_UNMARK,
			ACTION_LOG };

	private static IndexedContainer container;
	public TableLayout() {
		addComponent(table);

		// Label to indicate current selection
		final Label selected = new Label("No selection");
		addComponent(selected);

		// set a style name, so we can style rows and cells
		table.setStyleName("iso3166");

		// size
		table.setWidth("100%");
		table.setHeight("500px");

		// selectable
		table.setSelectable(true);
		table.setMultiSelect(true);
		table.setImmediate(true); // react at once when something is selected

		// connect data source
		container=new StaticDataAdaptor().getContainer();
		table.setContainerDataSource(container);

		// turn on column reordering and collapsing
		table.setColumnReorderingAllowed(true);
		table.setColumnCollapsingAllowed(true);

		// set column headers
		table.setColumnHeaders(new String[] { "Symbol", "Sector", "Industry" });

		// Column alignment
		table.setColumnAlignment(StaticDataAdaptor.TITLE_SECTOR,
				Table.ALIGN_CENTER);

		// Column width
		table.setColumnExpandRatio(StaticDataAdaptor.TITLE_SYMBOL, 1);
		table.setColumnWidth(StaticDataAdaptor.TITLE_SYMBOL, 70);
		table.setColumnWidth(StaticDataAdaptor.TITLE_SECTOR, 100);

		// Actions (a.k.a context menu)
		table.addActionHandler(new Action.Handler() {
			public Action[] getActions(Object target, Object sender) {
				if (markedRows.contains(target)) {
					return ACTIONS_MARKED;
				} else {
					return ACTIONS_UNMARKED;
				}
			}

			public void handleAction(Action action, Object sender, Object target) {
				if (ACTION_MARK.equals(action)) {
					markedRows.add(target);
					table.requestRepaint();
				} else if (ACTION_UNMARK.equals(action)) {
					markedRows.remove(target);
					table.requestRepaint();
				} else if (ACTION_LOG.equals(action)) {
					Item item = table.getItem(target);
					addComponent(new Label("Saved: "
							+ target
							+ ", "
							+ item.getItemProperty(
									StaticDataAdaptor.TITLE_SYMBOL).getValue()));
				}

			}

		});

		// style generator
		table.setCellStyleGenerator(new CellStyleGenerator() {
			public String getStyle(Object itemId, Object propertyId) {
				if (propertyId == null) {
					// no propertyId, styling row
					return (markedRows.contains(itemId) ? "marked" : null);
				} else if (StaticDataAdaptor.TITLE_SYMBOL.equals(propertyId)) {
					return "bold";
				} else {
					// no style
					return null;
				}

			}

		});

		// listen for valueChange, a.k.a 'select' and update the label
		table.addListener(new Table.ValueChangeListener() {
			public void valueChange(ValueChangeEvent event) {
				// in multiselect mode, a Set of itemIds is returned,
				// in singleselect mode the itemId is returned directly
				Set<?> value = (Set<?>) event.getProperty().getValue();
				if (null == value || value.size() == 0) {
					selected.setValue("No selection");
				} else {
					selected.setValue("Selected: " + table.getValue());
				}
			}
		});

	}
	public static final Object TITLE_SYMBOL = "symbol";
	public static final Object TITLE_SECTOR = "sector";
	public static final Object TITLE_INDUSTRY = "industry";
	private static DataNucleusUtil util = new DataNucleusUtil();
	private static List<StaticData> listOfRecords;
	public static void reloadData(Property property) {
		container = new IndexedContainer();
		container.addContainerProperty(TITLE_SYMBOL, String.class, null);
		container.addContainerProperty(TITLE_SECTOR, String.class, null);
		container.addContainerProperty(TITLE_INDUSTRY, String.class, null);

		
		// get full list by default
		listOfRecords = util.getRecordBySector(property.getValue().toString());		
		
		for (StaticData staticData : listOfRecords) {
			Item item = container.addItem(staticData.getSymbol());
			item.getItemProperty(TITLE_SYMBOL).setValue(staticData.getSymbol());
			item.getItemProperty(TITLE_SECTOR).setValue(staticData.getSector());
			item.getItemProperty(TITLE_INDUSTRY).setValue(
					staticData.getIndustry());
		}

		container.sort(new Object[] { TITLE_SYMBOL }, new boolean[] { true });
		table.setContainerDataSource(container);
	}
}